﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Telerik.Web.UI;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            selectCity();
            
        }
    }

    private void selectCity()
    {
        var city = new DealesDataContext();
        var query = city.Select_City();
        RadGrid1.DataSource = query;
        RadGrid1.DataBind();
    }
    protected void RadGrid1_DeleteCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        var CityID = (long)((GridDataItem)e.Item).GetDataKeyValue("CityID");
        var city = new DealesDataContext();
        var query = city.Delete_City(CityID);
        selectCity();
    }

    protected void RadGrid1_NeedDataSource(object source, GridNeedDataSourceEventArgs e)
    {
        var deal = new DealesDataContext();
        var query = deal.Select_City();
        RadGrid1.DataSource = query;
        
    }


    protected void RadGrid1_UpdateCommand(object source, GridCommandEventArgs e)
    {
        var editableItem = ((GridEditableItem)e.Item);
        var CityID = (long)editableItem.GetDataKeyValue("CityID");

        //retrive entity form the Db
        var city = new DealesDataContext();
        
        RadTextBox txtCity = e.Item.FindControl("txtCity") as RadTextBox;
        var query = city.Update_City(CityID, Convert.ToString(txtCity.Text));
    }


    protected void RadGrid1_InsertCommand(object source, GridCommandEventArgs e)
    {
        var editableItem = ((GridEditableItem)e.Item);
        //create new entity
        var city = new DealesDataContext();
        RadTextBox txtCity = e.Item.FindControl("txtCity") as RadTextBox;
        var query = city.Insert_City(txtCity.Text);
       
    }
}
